const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

/**
 * 将日期字符串转换显示方式
 * param  string 2018-01-01
 * return string 01年01日
 */
function formatDate(datetime) {
  let result = datetime.split(' ')
  let date = result[0].split('-')
  let time = result[1].split(':')

  return date[1] + "月" + date[2] + "日 "
}

function formatDateTime(datetime) {
  let result = datetime.split(' ')
  let date = result[0].split('-')
  let time = result[1].split(':')

  return date[1] + "月" + date[2] + "日 " + time[0] + ':' + time[1]
}

var hashMap = {
  Set: function (key, value) { this[key] = value },
  Get: function (key) { return this[key] },
  Contains: function (key) { return this.Get(key) == null ? false : true },
  Remove: function (key) { delete this[key] }
}

function checkEmailFormat(email) {
    // var reg = /^(\w)+(\.\w+)*@(\w)+((\.\w+)+)$/
    let reg = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/
    return reg.test(email)

}

module.exports = {
  formatTime: formatTime,
  formatDate:formatDate,
  formatDateTime: formatDateTime,
  hashMap: hashMap,
    checkEmailFormat:checkEmailFormat
}
